//
//  RootVC.m
//  sunna-iphone-customer
//
//  Created by David Hamrick on 3/25/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "RootVC.h"
#import "Passenger.h"
#import "CabDriverVC.h"

@implementation RootVC

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
	[super viewDidLoad];
	
	[_nameField setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"name"]];
	[_addressField setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"address"]];
	[_phoneField setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"phone"]];
	
	NSInteger passengerID = [[NSUserDefaults standardUserDefaults] integerForKey:@"passengerID"];
	p = [Passenger PassengerWithId:passengerID];
	if([p ID] != 0)
	{
		CabDriverVC* newVC = [[CabDriverVC alloc] init];
		newVC.passenger = p;
		[self.navigationController pushViewController:newVC animated:NO];
		return;
	}
	[_nameField becomeFirstResponder];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	[_nameField setText:@""];
	[_addressField setText:@""];
	[_phoneField setText:@""];
	
	[_nameField resignFirstResponder];
	[_addressField resignFirstResponder];
	[_phoneField resignFirstResponder];
	
	CabDriverVC* newVC = [[CabDriverVC alloc] init];
	newVC.passenger = p;
	[self.navigationController pushViewController:newVC animated:YES];
}


- (IBAction)buttonPressed:(id)sender
{
	p = [[Passenger alloc] init];
	[p setName:[_nameField text]];
	[p setAddress:[_addressField text]];
	[p setPhone:[_phoneField text]];
	[p save];
	[[NSUserDefaults standardUserDefaults] setInteger:[p ID] forKey:@"passengerID"];
	
	[[NSUserDefaults standardUserDefaults] setObject:[_nameField text] forKey:@"name"];
	[[NSUserDefaults standardUserDefaults] setObject:[_addressField text] forKey:@"address"];
	[[NSUserDefaults standardUserDefaults] setObject:[_phoneField text] forKey:@"phone"];

	UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Request Received"
																	message:nil 
																  delegate:self 
													  cancelButtonTitle:@"Ok"
													  otherButtonTitles:nil];
	[alert show];
}



- (void)dealloc 
{
    [super dealloc];
}


@end
